package com.hos.db.create.ill;

import com.hos.db.entity.ill.IllDataEntity;
import com.hos.db.entity.ill.IllVideoEntity;
import com.hos.utile.DLog;
import com.hos.utile.JsonUtile;
import com.hos.utile.StringUtile;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;

import java.util.Date;
import java.util.List;

/**
 * 影像资料
 * Created by guom on 2018/10/15.
 */
public class IllDataTable {
    public static String tableName = "pat_ill_datas";
    public static String tableIllId = "ill_id";//病例id
    public static String tableVerifyTime = "verify_time";//检验时间
    public static String tableVerifyDescribe = "verify_describe";//检验描述
    public static String tableVerifyUrls = "verify_urls";//检验url
    //
    public static String tableEcgTime = "ecg_time";//心电图时间
    public static String tableEcgDescribe = "ecg_describe";//心电图描述
    public static String tableEcgUrls = "ecg_urls";//心电图url
    //
    public static String tableUltrasoundTime = "ultrasound_time";//超声时间
    public static String tableUltrasoundDescribe = "ultrasound_describe";//超声描述
    public static String tableUltrasoundUrls = "ultrasound_urls";//超声url
    //
    public static String tableStillTime = "still_time";//静态影像时间
    public static String tableStillDescribe = "still_describe";//静态影像描述
    public static String tableStillUrls = "still_urls";//静态影像url
    //
    public static String tableMoveTime = "move_time";//动态影像时间
    public static String tableMoveDescribe = "move_describe";//动态影像描述
    public static String tableMovUrls = "move_urls";//动态影像url
    public static String tableCreateTime = "create_time";// 病例创建时间

    //
    public static String careteTable = "create table  "
            + tableName + " ( "
            + tableIllId + "  varchar(200), "
            + tableVerifyTime + " varchar(200), "
            + tableVerifyDescribe + " longText, "
            + tableVerifyUrls + " longText, "
            + tableEcgTime + " varchar(200), "
            + tableEcgDescribe + " longText, "
            + tableEcgUrls + " longText, "
            + tableUltrasoundTime + " varchar(200), "
            + tableUltrasoundDescribe + " longText, "
            + tableUltrasoundUrls + " longText, "
            + tableStillTime + " varchar(200), "
            + tableStillDescribe + " longText, "
            + tableStillUrls + " longText, "
            + tableMoveTime + " varchar(200), "
            + tableMoveDescribe + " longText, "
            + tableMovUrls + " longText, "
            + tableCreateTime + " datetime "
            + "); ";

    public static boolean onInsert(JdbcTemplate jdbcTemplate, String illId, IllDataEntity bean) {
        if (bean == null) {
            bean = new IllDataEntity();
        }
        String sql = "INSERT INTO " + IllDataTable.tableName
                + "("
                + IllDataTable.tableIllId + ","
                + IllDataTable.tableVerifyTime + ","
                + IllDataTable.tableVerifyDescribe + ","
                + IllDataTable.tableVerifyUrls + ","
                + IllDataTable.tableEcgTime + ","
                + IllDataTable.tableEcgDescribe + ","
                + IllDataTable.tableEcgUrls + ","
                + IllDataTable.tableUltrasoundTime + ","
                + IllDataTable.tableUltrasoundDescribe + ","
                + IllDataTable.tableUltrasoundUrls + ","
                + IllDataTable.tableStillTime + ","
                + IllDataTable.tableStillDescribe + ","
                + IllDataTable.tableStillUrls + ","
                + IllDataTable.tableMoveTime + ","
                + IllDataTable.tableMoveDescribe + ","
                + IllDataTable.tableMovUrls + ","
                + IllDataTable.tableCreateTime
                + ")"
                + " VALUES (?,?,?, ?,?,?, ?,?,?,?, ?,?,?, ?,?,?,?)";
        String value = "";
        List<IllVideoEntity> videos = bean.getMoveUrl();
        if (videos != null && videos.size() > 0) {
            value = JsonUtile.obj2Json(videos);
        }
        Object[] obj = new Object[]{illId,
                bean.getVerifyTime(), bean.getVerifyDescribe(), StringUtile.getString(bean.getVerifyUrl()),
                bean.getEcgTime(), bean.getEcgDescribe(), StringUtile.getString(bean.getEcgUrl()),
                bean.getUltrasoundTime(), bean.getUltrasoundDescribe(), StringUtile.getString(bean.getUltrasoundUrl()),
                bean.getStillTime(), bean.getStillDescribe(), StringUtile.getString(bean.getStillUrl()),
                bean.getMoveTime(), bean.getMoveDescribe(), value, new Date()};
        jdbcTemplate.update(sql, obj);
        return true;
    }

    //删除
    public static boolean onDlete(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "DELETE  FROM " + IllDataTable.tableName
                + " WHERE "
                + IllDataTable.tableIllId + " = '" + illId + "'";
        int state = jdbcTemplate.update(sql);
        return state > 0;
    }

    //修改
    public static boolean onUpdate(JdbcTemplate jdbcTemplate, String illId, IllDataEntity bean) {
        if (bean == null) {
            return true;
        }
        String update = "";
        String value = bean.getVerifyTime();
        if (value != null) {
            update += " , " + IllDataTable.tableVerifyTime + " = '" + value + "'";
        }
        value = bean.getVerifyDescribe();
        if (value != null) {
            update += " , " + IllDataTable.tableVerifyDescribe + " = '" + value + "'";
        }
        value = StringUtile.getString(bean.getVerifyUrl());
        if (value != null) {
            update += " , " + IllDataTable.tableVerifyUrls + " = '" + value + "'";
        }
        //
        value = bean.getEcgTime();
        if (value != null) {
            update += " , " + IllDataTable.tableEcgTime + " = '" + value + "'";
        }
        value = bean.getEcgDescribe();
        if (value != null) {
            update += " , " + IllDataTable.tableEcgDescribe + " = '" + value + "'";
        }
        value = StringUtile.getString(bean.getEcgUrl());
        if (value != null) {
            update += " , " + IllDataTable.tableEcgUrls + " = '" + value + "'";
        }
        //
        value = bean.getUltrasoundTime();
        if (value != null) {
            update += " , " + IllDataTable.tableUltrasoundTime + " = '" + value + "'";
        }
        value = bean.getUltrasoundDescribe();
        if (value != null) {
            update += " , " + IllDataTable.tableUltrasoundDescribe + " = '" + value + "'";
        }
        value = StringUtile.getString(bean.getUltrasoundUrl());
        if (value != null) {
            update += " , " + IllDataTable.tableUltrasoundUrls + " = '" + value + "'";
        }
        //
        value = bean.getStillTime();
        if (value != null) {
            update += " , " + IllDataTable.tableStillTime + " = '" + value + "'";
        }
        value = bean.getStillDescribe();
        if (value != null) {
            update += " , " + IllDataTable.tableStillDescribe + " = '" + value + "'";
        }
        value = StringUtile.getString(bean.getStillUrl());
        if (value != null) {
            update += " , " + IllDataTable.tableStillUrls + " = '" + value + "'";
        }
        //
        value = bean.getMoveTime();
        if (value != null) {
            update += " , " + IllDataTable.tableMoveTime + " = '" + value + "'";
        }
        value = bean.getMoveDescribe();
        if (value != null) {
            update += " , " + IllDataTable.tableMoveDescribe + " = '" + value + "'";
        }
        value = null;
        List<IllVideoEntity> videos = bean.getMoveUrl();
        if (videos != null) {
            if (videos.size() == 0) {
                value = "";
            }
            if (videos.size() > 0) {
                value = JsonUtile.obj2Json(videos);
            }
        }

        if (value != null) {
            update += " , " + IllDataTable.tableMovUrls + " = '" + value + "'";
        }

        if (update.equals("")) {
            return true;
        }
        update = update.replaceFirst(" , ", "");
        String sql = "UPDATE " + IllDataTable.tableName
                + " SET "
                + update
                + " WHERE "
                + IllDataTable.tableIllId + " = '" + illId + "'";
        int s = jdbcTemplate.update(sql);
        return s > 0;
    }

    public static IllDataEntity getResults(JdbcTemplate jdbcTemplate, String illId) {
        String sql = "SELECT * FROM " + IllDataTable.tableName
                + " WHERE "
                + IllDataTable.tableIllId + " = '" + illId + "'";
        IllDataEntity entity = new IllDataEntity();
        List<IllDataEntity> ills = onQuery(jdbcTemplate, sql);
        if (ills != null && ills.size() > 0) {
            entity = ills.get(0);
        }
        return entity;
    }


    private static List<IllDataEntity> onQuery(JdbcTemplate jdbcTemplate, String sql) {
        List<IllDataEntity> ills = null;
        try {
            ills = jdbcTemplate.query(sql,
                    ParameterizedBeanPropertyRowMapper.newInstance(IllDataEntity.class));
            DLog.e("查询成功：==========");
        } catch (Exception e) {
            DLog.e("查询失败：" + e.getMessage());
        }
        return ills;
    }
}
